
//
//  MyCenterViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/18.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "MyCenterViewController.h"
#import "UserInfoHeaderCell.h"
#import "CenterItemCell.h"
#import "PersonalInfoViewController.h"
#import "SetupViewController.h"
#import "RedPacketViewController.h"
#import "CouponsViewController.h"
#import "WalletViewController.h"
#import "FeedbackViewController.h"
#import "IdentifyAndMaintainViewController.h"
#import "ConOrReViewController.h"
#import "LoginViewController.h"
#import "MyAddressListViewController.h"
#import "MessageListViewController.h"
#import "CustomerServiceViewController.h"
#import "InvitationViewController.h"
#import "AllOrdersViewController.h"
#import "UserInfoModel.h"
NSString * const UserInfoHeaderCellIdentifier = @"UserInfoHeaderCellIdentifier";
NSString * const CenterItemCellIdentifier = @"CenterItemCellIdentifier";
@interface MyCenterViewController ()
@property(nonatomic, assign) BOOL isLoginState;
@end

@implementation MyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.collectionView.contentInset = UIEdgeInsetsZero;
    self.collectionView.scrollIndicatorInsets = UIEdgeInsetsZero;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self setupRightItem];

}

- (void)setupRightItem{
    
    UIImage *rightBtnImg = [[UIImage imageNamed:@"btn_xiaoxibai"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]   ;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:rightBtnImg style:UIBarButtonItemStylePlain target:self action:@selector(gotoMessageAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (BOOL )isShowBackItem{
    return NO;
}
- (void)registerCell{
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([UserInfoHeaderCell class]) bundle:nil] forCellWithReuseIdentifier:UserInfoHeaderCellIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CenterItemCell class]) bundle:nil]  forCellWithReuseIdentifier:CenterItemCellIdentifier];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableViewHeader"];
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger row = 0;
    if (section == 0) {
        row = 1;
    }else if (section == 1){
        row = [self getFirstItems].count;
    }else if (section == 2){
        row = [self getSecondItems].count;
    }
    return row;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = nil;
    if (indexPath.section == 0) {
        UserInfoHeaderCell * tempCell = [collectionView dequeueReusableCellWithReuseIdentifier:UserInfoHeaderCellIdentifier forIndexPath:indexPath];
       
        [self configUserInfoCell:tempCell withIndexPath:indexPath];
        cell = tempCell;
    }else {
        CenterItemCell * tempCell = [collectionView dequeueReusableCellWithReuseIdentifier:CenterItemCellIdentifier forIndexPath:indexPath];
        [self configItemCell:tempCell withIndexPath:indexPath];
        cell = tempCell;
    }
    
    return cell;
}

- (void)configUserInfoCell:(UserInfoHeaderCell *)cell  withIndexPath:(NSIndexPath *)indexPath{
    [cell setupUserInfo:UserInfo];
    cell.touchBlock = ^(NSInteger touchType) {
        if (touchType == touchType_personal) {
            [self  gotoPersonalInfo];
 
        }else if(touchType == touchType_discount){
            [self  gotoDiscountInfo];
        }else if(touchType == touchType_remaining){
            [self  gotoRemainingInfo];
        }else if(touchType == touchType_redPacket){
            [self  gotoRedPacketInfo];
        }
    };
}

- (void)configItemCell:(CenterItemCell *)cell  withIndexPath:(NSIndexPath *)indexPath{
    
    NSArray * itemS = nil;
    if (indexPath.section == 1) {
        itemS = [self getFirstItems];
    }else if(indexPath.section == 2){
        itemS = [self getSecondItems];
    }
    [cell setupItem:itemS[indexPath.row]];
}
- (NSArray *)getFirstItems{
    
    NSDictionary * item1 = @{@"title":@"待付款",@"icon":@"geren_daifukuan"};
    NSDictionary * item2 = @{@"title":@"待发货",@"icon":@"geren_daifahuo"};
    NSDictionary * item3 = @{@"title":@"待收货",@"icon":@"geren_daishouhuo"};
    NSDictionary * item4 = @{@"title":@"退款/售后",@"icon":@"geren_tuikuan"};
    NSDictionary * item5 = @{@"title":@"全部",@"icon":@"geren_quanbu"};
   
    NSArray * tempArray = @[item1,item2,item3,item4,item5];
    return tempArray;
}
- (NSArray *)getSecondItems{
    NSDictionary * item1 = @{@"title":@"我的鉴定",@"icon":@"geren_wodejianding"};
    NSDictionary * item2 = @{@"title":@"我的养护",@"icon":@"geren-wodeyanghu"};
    NSDictionary * item3 = @{@"title":@"寄卖/回收",@"icon":@"geren_jimai"};
    NSDictionary * item4 = @{@"title":@"我的地址",@"icon":@"geren_dizhi"};
    NSDictionary * item5 = @{@"title":@"我的客服",@"icon":@"geren_kefu"};
    NSDictionary * item6 = @{@"title":@"意见反馈",@"icon":@"geren_yijian"};
    NSDictionary * item7 = @{@"title":@"邀请好友",@"icon":@"geren_yaoqing"};
    NSDictionary * item8 = @{@"title":@"设置",@"icon":@"geren_shezhi"};
 
    
    NSArray * tempArray = @[item1,item2,item3,item4,item5,item6,item7,item8];
    return tempArray;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize =  CGSizeZero;
    CGFloat iphoneW = [UIScreen mainScreen].bounds.size.width;
    if (indexPath.section == 0) {
        cellSize = CGSizeMake(iphoneW, 160 + NAVIGATOR_HEIGHT);
    }else if (indexPath.section == 1){
        
        cellSize = CGSizeMake(iphoneW/5, 100);
    }else if (indexPath.section == 2){
        
        cellSize = CGSizeMake(iphoneW/4, 100);
    }
    return cellSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
     CGFloat iphoneW = [UIScreen mainScreen].bounds.size.width;
    CGSize size = CGSizeZero;
    if (section == 2) {
        size = CGSizeMake(iphoneW, 8);
    }
    return size;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableViewHeader" forIndexPath:indexPath];
        if(headerView == nil)
        {
            headerView = [[UICollectionReusableView alloc] init];
        }
        headerView.backgroundColor = BACKGROUND_COLOR;

        return headerView;
    }
  
    return nil;
}
// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
        if (indexPath.section == 1) {
            [self toucheOrderItem:indexPath];
        }else if (indexPath.section == 2){
            [self toucheOtherItem:indexPath];
        }
    
  
}


#pragma mark -----

- (void)toucheOrderItem:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self gotoWaitPaymentVC];
            break;
        case 1:
            [self gotoWaitSendGoodsVC];
            break;
        case 2:
            [self gotoWaitGoodsVC];
            break;
        case 3:
            [self gotoRefundVC];
            break;
        case 4:
            [self gotosAllVC];
            break;
   
        default:
            break;
    }
    
}
//待付款
- (void)gotoWaitPaymentVC{
    AllOrdersViewController * tempVC = [[AllOrdersViewController alloc]initWithStyle:AllOrdersStyle_waitPayment];
    [self pushViewController:tempVC];
}
//待发货
- (void)gotoWaitSendGoodsVC{
    AllOrdersViewController * tempVC = [[AllOrdersViewController alloc]initWithStyle:AllOrdersStyle_waitSend ];
    [self pushViewController:tempVC];
}
//待收货
- (void)gotoWaitGoodsVC{
    AllOrdersViewController * tempVC = [[AllOrdersViewController alloc]initWithStyle:AllOrdersStyle_waitGoods];
    [self pushViewController:tempVC];
}
//退款
- (void)gotoRefundVC{
    AllOrdersViewController * tempVC = [[AllOrdersViewController alloc]initWithStyle:AllOrdersStyle_refundReview];
    [self pushViewController:tempVC];
}
//全部
- (void)gotosAllVC{
    
    AllOrdersViewController * tempVC = [[AllOrdersViewController alloc]initWithStyle:AllOrdersStyle_all];
    [self pushViewController:tempVC];
}

- (void)toucheOtherItem:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
            [self gotoIdentificationVC];
            break;
        case 1:
            [self gotoMaintenanceVC];
            break;
        case 2:
            [self gotoConsignmentVC];
            break;
        case 3:
            [self gotoAdressVC];
            break;
        case 4:
            [self gotosServiceVC];
            break;
        case 5:
            [self gotoFeedbackVC];
            break;
        case 6:
            [self gotoInvitationVC];
            break;
        case 7:
            [self gotoSetupVC];
            break;
        default:
            break;
    }
    
}
//鉴定
- (void)gotoIdentificationVC{
    IdentifyAndMaintainViewController * tempVC = [[IdentifyAndMaintainViewController alloc]initWithStyle:ViewStyle_Identify];
    [self.navigationController pushViewController:tempVC animated:YES];
}
//养护
- (void)gotoMaintenanceVC{
    IdentifyAndMaintainViewController * tempVC = [[IdentifyAndMaintainViewController alloc]initWithStyle:ViewStyle_Maintain];
    [self.navigationController pushViewController:tempVC animated:YES];
}
//寄卖/回收
- (void)gotoConsignmentVC{
    ConOrReViewController * tempVC = [[ConOrReViewController alloc]initWithStyle:ConOrReViewStyle_recycle];
    [self.navigationController pushViewController:tempVC animated:YES];
}
//地址
- (void)gotoAdressVC{
    MyAddressListViewController *tempVC = [[MyAddressListViewController alloc]init];
    [self pushViewController:tempVC];
}
//客服
- (void)gotosServiceVC{
    CustomerServiceViewController * tempVC = [[CustomerServiceViewController alloc]init];
    [self pushViewController:tempVC];
}
//意见反馈
- (void)gotoFeedbackVC{
    
    FeedbackViewController *setupvc = [[FeedbackViewController alloc]initWithNibName:NSStringFromClass([FeedbackViewController class]) bundle:nil];
    [self.navigationController pushViewController:setupvc animated:YES];
}
//邀请好友
- (void)gotoInvitationVC{
    
    InvitationViewController * tempVC = [[InvitationViewController alloc]init];
    [self pushViewController:tempVC];
}
//设置
- (void)gotoSetupVC{
    
    SetupViewController *setupvc = [[SetupViewController alloc]init];
    [self.navigationController pushViewController:setupvc animated:YES];
}
#pragma mark ------
// 优惠券
- (void)gotoDiscountInfo{
    CouponsViewController * couponsVC = [[CouponsViewController alloc]init];
    [self.navigationController pushViewController:couponsVC animated:YES];
    
}
//余额
- (void)gotoRemainingInfo{
    WalletViewController * walletVC = [[WalletViewController alloc]init];
    [self.navigationController pushViewController:walletVC animated:YES];
}
//红包
- (void)gotoRedPacketInfo{
    RedPacketViewController * redPacketVC = [[RedPacketViewController alloc]init];
    [self.navigationController pushViewController:redPacketVC animated:YES];
    
}
//用户信息
- (void)gotoPersonalInfo{
    PersonalInfoViewController * infoVC = [[PersonalInfoViewController alloc]init];
    [self.navigationController pushViewController:infoVC animated:YES];
    
}


// 消息
- (void)gotoMessageAction{
    
    MessageListViewController * messageVC = [[MessageListViewController alloc]init];
    [self pushViewController:messageVC];
}
- (BOOL)getNavBarBgHidden{
    return YES;
}

- (BOOL)getAutomaticallyAdjustsScrollViewInsets{
    
    return NO;
}
- (BOOL)getTranslucent{
    return YES;
}
- (UIRectEdge)getViewRect{
    return UIRectEdgeAll;
}
- (CGRect)getCollectionViewFrame{
    
    return CGRectMake(0, -NAVIGATOR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height + NAVIGATOR_HEIGHT);
}

#pragma mark -----
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self updateUserInfo];
}
- (void)updateUserInfo{
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"User/getUserinfo" params:@{@"token":UserInfo.userToken} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        if (responseObject) {
             NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
             [self saveUserInfo:userinfo];
        }
        [strongSelf.collectionView reloadData];
    }];
}

- (void)saveUserInfo:(NSDictionary *)userinfo{
    
    UserInfo.avatar_url = [self filterEmpty:userinfo[@"avatar"]];
    UserInfo.userToken =   [self filterEmpty:userinfo[@"token"]];
    UserInfo.userId = [self filterEmpty:userinfo[@"user_id"]];
    UserInfo.nickname = [self filterEmpty:userinfo[@"nickname"]];
    UserInfo.mobile = [self filterEmpty:userinfo[@"mobile"]];
    UserInfo.gender =  [self filterEmpty:userinfo[@"gender"]];
    UserInfo.birthday = [self filterEmpty:userinfo[@"birthday"]];
    UserInfo.redpack_num = [self filterEmpty:userinfo[@"redpack_num"]];
    UserInfo.coupon_num =  [self filterEmpty:userinfo[@"coupon_num"]];
    UserInfo.wallet_balance = [self filterEmpty:userinfo[@"wallet_balance"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"USER_LOGIN_SUCCESSFUL" object:nil];
    
}
- (NSString *)filterEmpty:(id)parameter{
    NSString * str = @"";
    if (parameter &&![parameter isEqual:[NSNull null]] ) {
        str = parameter;
    }else{
        str = @"";
    }
    return str;
}



@end
